/**
 ******************************************************************************
 * @file           : main.c
 * @brief          : ADC Multi-Channel Example
 ******************************************************************************
 * @attention
 *
 * ADC多通道采集示例
 * 演示DMA自动传输、多通道扫描、内部温度传感器
 *
 ******************************************************************************
 */

/* Includes ------------------------------------------------------------------*/
#include "stm32f1xx_hal.h"
#include "./BSP/ADC/bsp_adc.h"
#include "./BSP/USART/bsp_usart.h"
#include <stdio.h>

/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
void Display_ADC_Values(void);

/* Private variables ---------------------------------------------------------*/
#define CHANNEL_NUM  4

/**
 * @brief  主函数
 */
int main(void)
{
    /* HAL库初始化 */
    HAL_Init();
    
    /* 系统时钟配置 */
    SystemClock_Config();
    
    /* 初始化串口 */
    usart_init(115200);
    
    printf("\r\n========================================\r\n");
    printf("  ADC多通道采集示例\r\n");
    printf("========================================\r\n");
    printf("通道配置:\r\n");
    printf("  CH0 (PA0) - 电位器/传感器1\r\n");
    printf("  CH1 (PA1) - 传感器2\r\n");
    printf("  CH2 (PA2) - 传感器3\r\n");
    printf("  Temp      - 内部温度传感器\r\n");
    printf("========================================\r\n\r\n");
    
    /* 初始化ADC */
    adc_init();
    
    /* 启动DMA采集 */
    adc_start_dma();
    
    /* 主循环 */
    while (1)
    {
        /* 显示ADC采集值 */
        Display_ADC_Values();
        
        /* 延时1秒 */
        HAL_Delay(1000);
    }
}

/**
 * @brief  显示ADC采集值
 */
void Display_ADC_Values(void)
{
    uint16_t adc_values[CHANNEL_NUM];
    float voltages[CHANNEL_NUM];
    float temperature;
    
    /* 读取ADC值 */
    adc_get_values(adc_values, CHANNEL_NUM);
    
    /* 转换为电压值（0~3.3V） */
    for (int i = 0; i < CHANNEL_NUM - 1; i++)
    {
        voltages[i] = (float)adc_values[i] * 3.3f / 4095.0f;
    }
    
    /* 读取温度 */
    temperature = adc_get_temperature();
    
    /* 打印结果 */
    printf("┌────────────────────────────────────┐\r\n");
    printf("│ ADC采集结果                         │\r\n");
    printf("├────────────────────────────────────┤\r\n");
    printf("│ CH0: %4d  →  %.3fV              │\r\n", adc_values[0], voltages[0]);
    printf("│ CH1: %4d  →  %.3fV              │\r\n", adc_values[1], voltages[1]);
    printf("│ CH2: %4d  →  %.3fV              │\r\n", adc_values[2], voltages[2]);
    printf("│ 温度: %.1f°C                      │\r\n", temperature);
    printf("└────────────────────────────────────┘\r\n\r\n");
}

/**
 * @brief  系统时钟配置
 */
void SystemClock_Config(void)
{
    RCC_OscInitTypeDef RCC_OscInitStruct = {0};
    RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
    RCC_PeriphCLKInitTypeDef PeriphClkInit = {0};
    
    RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
    RCC_OscInitStruct.HSEState = RCC_HSE_ON;
    RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;
    RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
    RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
    RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9;
    HAL_RCC_OscConfig(&RCC_OscInitStruct);
    
    RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_SYSCLK
                                | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2;
    RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
    RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
    RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
    RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
    HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2);
    
    /* ADC时钟配置 */
    PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_ADC;
    PeriphClkInit.AdcClockSelection = RCC_ADCPCLK2_DIV6;  /* 72MHz/6=12MHz */
    HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit);
}

int fputc(int ch, FILE *f)
{
    usart_send_byte((uint8_t)ch);
    return ch;
}
